<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="120px">
        <el-form-item label="产品码/箱码" prop="code">
          <el-input v-model="formData.code" placeholder="请输入产品码/箱码" size="small"
                    style="width: 200px; margin-right: 10px;"/>
          <el-button @click="" size="small">扫码</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {DICT_TYPE} from "@/utils/dict";

export default {
  name: "TrayForm",
  computed: {
    DICT_TYPE() {
      return DICT_TYPE
    }
  },
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        code: undefined,
      },
      // 表单校验
      formRules: {
        code: [{required: true, message: '编码不能为空', trigger: 'blur'}],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open() {
      this.dialogVisible = true;
      this.reset();
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        this.dialogVisible = false;
        this.$emit('success', this.formData.code);
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        code: undefined,
      };
      this.list = [];
      this.resetForm("formRef");
    }
  }
};
</script>
